<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
class OrderController extends PublicController {
	
	
    public function indexAction(){
		
		$session=session('user');
		
		if(empty($session)){
			$this->success('请先登陆', U('user/login'));
			die();
		}
			
		$Model=D('Order');
		
		$list=$Model->select();
		
		
		$total=0;
		foreach($list as $k=>$v){
			
			$list[$k]['detail']=json_decode($v['detail'],true);
			$total+=$list[$k]['detail']['subtotal'];
		}
		
		//print_r($list);die;
		 $this->assign('total',$total);
		 $this->assign('list',$list);
		
		
		$this->display();
	
    }
	
	
	//add订单
	public function addorderAction(){
		$session=session('user');
		if(empty($session)){
			$this->success('请先登陆', U('user/login'));
			die();
		}
		$key=I('post.key');
		
		if(empty($key)){
			$this->success('请选择需要购买的商品', U('cart/index'));
			die();
		}
		
		//print_r($key);die;

		$Model=D('Order');
		
		$catr=session('cart');
		
		$order=array();
		foreach($catr as $c=>$d){
			foreach($key as $a=>$b){
				$order[$b]=$d;
			}
				
		}
		
		//print_r($order);die;
		
		if(!empty($order)){
			$arr=array();
			foreach($order as $k=>$v){
				
				$arr['number']=$v['number'];
				
				$arr['detail']=json_encode($order[$k]);
				
				$arr['addtime']=time();
				
				$arr['uid']=$session['uid'];
				
				$one=$Model->add($arr);
				
			}
			
			if($one>0){
					foreach($key as $a=>$b){
					 	unset($catr[$b]);
					}
					session('cart',$catr);
					$this->success('提交订单成功', U('order/index'));
				}else{
					$this->success('提交订单失败', U('cart/index'));
			}
		}
		
		
		//print_r($order);die;
		
		
	}
	
	
		
	
	
}